package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCartMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

@Service
public class LitemallCartService {
    @Resource
    private LitemallCartMapper cartMapper;
    @Autowired
    private LitemallCommodityService litemallCommodityService;

    public LitemallCart queryExist(Integer goodsId, Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andCommodityIdEqualTo(goodsId).andUserIdEqualTo(userId);
        return cartMapper.selectOneByExample(example);
    }

    public void add(LitemallCart cart) {
        cart.setAddTime(LocalDateTime.now());
        cartMapper.insertSelective(cart);
    }

    public int updateById(LitemallCart cart) {
        return cartMapper.updateByPrimaryKeySelective(cart);
    }

    public List<LitemallCart> queryByUid(int userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId);
        return cartMapper.selectByExample(example);
    }

    public int delete(List<Integer> goodIds, int userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId).andCommodityIdIn(goodIds);
        return cartMapper.deleteByExample(example);
    }

    public LitemallCart findById(Integer id) {
        return cartMapper.selectByPrimaryKey(id);
    }

    public void clearGoods(Integer userId) {
        LitemallCartExample example = new LitemallCartExample();
        example.or().andUserIdEqualTo(userId);
        LitemallCart cart = new LitemallCart();

        cartMapper.updateByExampleSelective(cart, example);
    }

    public List<LitemallCart> querySelective(Integer userId, Integer goodsId, Integer page, Integer limit, String sort, String order) {
        LitemallCartExample example = new LitemallCartExample();
        LitemallCartExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtils.isEmpty(goodsId)) {
            criteria.andCommodityIdEqualTo(goodsId);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return cartMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        cartMapper.deleteByPrimaryKey(id);
    }

    public List<CartMsg> findCardByUserId(Integer userId) {
        return cartMapper.findCardByUserId(userId);
    }

    public List<LitemallOrderGoods> findGoods(List<Integer> idList) {
        List<LitemallOrderGoods> list = new LinkedList<>();
        for (Integer id : idList) {
            LitemallOrderGoods temp = new LitemallOrderGoods();
            LitemallCart cart = findById(id);
            LitemallCommodity commodity = litemallCommodityService.selectById(cart.getCommodityId());
            temp.setGoodsId(cart.getCommodityId());
            temp.setNumber(cart.getCommodityNum());
            temp.setPicUrl(commodity.getCommodityMainImage());
            temp.setGoodsName(commodity.getCommodityName());
            temp.setGoodsPriceOrigin(commodity.getCommodityPrice());
            list.add(temp);
        }
        return list;
    }
}
